<?php
require_once (dirname(__FILE__).'/include/common.inc.php');


# 页面级别
# 0 = 所有人都可以访问
$conf_web_page_level = 0; 

class myPage extends page{
	public $sqlplus = "";
	public $qs = '';
	public $pageno = 1;
	public $pagenumber = 0;
	public $pagearr = array();
	
	public $cateId = false;
	
	function __construct(){
		global $config;
		parent::__construct();
		
		if(isset($_GET['page'])){
			$this->pageno = $_GET['page'];
		}
		
		if(isset($_GET['cate'])){
			$this->cateId = explode(",",$_GET['cate']);
			
		}
		
		if($this->cateId == true){
			
			$cnt = count($this->cateId);
			if($cnt == 1){
				$this->qs = "&cate=".$this->cateId[0];
				$this->sqlplus = " WHERE cs2_prod.cateId =".$this->cateId[0]." ";
			}else{
				$this->qs = "&cate=";
				$this->sqlplus = " WHERE ";
				for($i=0;$i<$cnt;$i++){
					$this->qs = $this->qs.$this->cateId[$i];
					$this->sqlplus = $this->sqlplus."cs2_prod.cateId =".$this->cateId[$i];
					if($i < $cnt-1){
						$this->qs = $this->qs.",";
						$this->sqlplus = $this->sqlplus." OR ";
					}
				}
			}
			
		}
		$config->smarty->assign('page', $this->pageno);
		$config->smarty->assign('qs', $this->qs);
	}
	function len(){
		global $config;
		$l = 0;

		$sql = "SELECT COUNT(*) as cnt FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId) ";
		$sql = $sql.$this->sqlplus;
		$row = $this->database->get_one($sql);
		
		$l = $row['cnt'];
		
		$this->pagenumber = $l / config::conf_web_pagecount;
		
		$config->smarty->assign('pagenumber', $this->pagenumber);
		
		for($i=1; $i<$this->pagenumber + 1; $i++){
			array_push($this->pagearr,$i);
		}
		$config->smarty->assign('pagearr', $this->pagearr);

		return $l;
	}
	
	function cate_listview(){
		$lv;
		try{
			$sql = "SELECT name, cateId
				FROM cs2_cate 
				WHERE (cateId=44 OR cateId=45 OR cateId=47 OR cateId=48 OR cateId=52 OR cateId=56 OR cateId=46 OR cateId=58)";
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			$lv = null;
		}
		return $lv;
	
	}
	function prod_listview(){ 
		$lv;
		try{
			$sql = "SELECT hsellId,cs2_hsell.prodId, cs2_prod.name, cs2_prod.model, cs2_hsell.title,
					cs2_prod.info,cs2_prod.barcode,cs2_prod.size_l,cs2_prod.size_w,
					cs2_prod.size_h,weight,cs2_prod.cateId,icon,iconinfo,num,contents,price,sprice,
				IFNULL((SELECT cs2_image.fileId FROM cs2_image 
					WHERE cs2_image.prodId=cs2_prod.prodId AND cs2_image.defId=0 LIMIT 0,1),'no_image') AS fileId
				FROM cs2_hsell LEFT JOIN cs2_prod ON (cs2_prod.prodId = cs2_hsell.prodId)
			";
			
			/*if($this->cateId != 0){
				$sql = $sql." WHERE (cateId=".$this->cateId.")";
			}*/
			$sql = $sql.$this->sqlplus;
			$sql = $sql." ORDER BY cateId LIMIT " . ($this->pageno-1) * config::conf_web_pagecount ." , ".config::conf_web_pagecount." ;";
			
			$lv = $this->database->select($sql);
		}catch (Exception $e) {
			#$lv = null;
		}
		return $lv;
	}
	function ajax_callback(){
		parent::ajax_callback();
		return json_encode($this->ajax_json);
	}
}
$pg = new myPage();
# 载入页面
if($_SERVER['REQUEST_METHOD'] != 'POST'){
	$config->smarty->assign('pagecount', config::conf_web_pagecount);
	$config->smarty->assign('len', $pg->len());
	$config->smarty->assign('cate_listview', $pg->cate_listview());
	$config->smarty->assign('prod_listview', $pg->prod_listview());
	# 加载页面
	$pg->render($config->temp_page_path);
}else{
	print_r($pg->ajax_callback());
}


?>